package com.bmp.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bmp.model.Groups;
import com.bmp.model.User;
import com.bmp.model.vo.UserVoGroups;
import com.bmp.service.GroupsManager;
import com.bmp.service.UserManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private int id;
	private String username;
	private String password;
	private User user;	
	private Map<String,Object> session = ActionContext.getContext().getSession();
	private UserManager userManager;
	private GroupsManager groupsManager;
	
	private JSONObject jsonObject;
	private String page;
	private String rows;
	//初始化UserManager和GroupsManager
	public UserAction(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		userManager = (UserManager)ctx.getBean("userManager");
		groupsManager = (GroupsManager)ctx.getBean("groupsManager");
	}
	
	@Override
	public String execute() throws Exception {
		User u = new User();
		Groups g = new Groups();
		u.setUsername(username);
		u.setPassword(password);
		u = this.userManager.getUserByNameAndPassword(username, password);
		//System.out.println("useraction");
		if(userManager.isUser(username, password)){
			g = this.groupsManager.getGroupsById(u.getG_id());
			ActionContext.getContext().getSession().put("user", u);//把user信息保存到session中
			ActionContext.getContext().getSession().put("groups", g);//把user对应的组信息保存到session中
			return "success";
		}
		return "fail";
	}
	//显示个人信息
	public String load(){
		System.out.println("hello");
		User u = (User)ActionContext.getContext().getSession().get("user");
		this.user=this.userManager.getUserById(u.getId());
		return "getMyInfo";
	}
	//显示所有用户信息列表
	public String userInfo(){
		System.out.println("hello userInfo");
		System.out.println("rows="+rows);
		//当前页  
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数          
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        //每页的开始记录  第一页为1  第二页为number +1   
        int start = (intPage-1)*number;        
        List<UserVoGroups> userList = userManager.getUser(start, number);//每页的数据，放入list        
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
        jsonMap.put("total", userManager.getTotalPages());//total键 存放总记录数，必须的     
        jsonMap.put("rows", userList);//rows键 存放每页记录 list
        System.out.println("intPage="+intPage);
        jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        System.out.println("number="+number);
		return "userInfo";
	}
	//删除用户
	public String deleteUser(){		
		System.out.println(user.getId());		
		userManager.deleteUserById(user.getId());
		return "success";
	}
	//增加用户
	public String addUser(){
		userManager.addUser(this.user);
		return "success";
	}
	//更新用户信息
	public String updateUser(){
		userManager.updateUser(this.user);
		return "success";
	}
	//修改用户密码
	public String updatePassword(){
		User user = (User)ActionContext.getContext().getSession().get("user");
		user.setPassword(password);
		userManager.updateUser(user);
		return "success";
	}
	/****************** getter and setter *********************/
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUsername() {
		return username;
	}
	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public String getPassword() {
		return password;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public String getPage() {
		return page;
	}

	public String getRows() {
		return rows;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public GroupsManager getGroupsManager() {
		return groupsManager;
	}

	public void setGroupsManager(GroupsManager groupsManager) {
		this.groupsManager = groupsManager;
	}

}
